package com.pri.chat.fragment;

import android.graphics.Typeface;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.netease.nim.uikit.net.entity.City;
import com.netease.nim.uikit.net.entity.DTBean;
import com.pri.chat.MainActivity;
import com.pri.chat.R;
import com.pri.chat.activity.ReleaseDTActivity;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.fragment.FragmentLazy;
import com.vondear.rxui.view.dialog.RxDialogShaiXuan;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 描述：添加类的描述
 * @author wx5e48d604c2885118
 * @time 2020/4/3
 */
public class MainFragment extends FragmentLazy {
    Unbinder unbinder;
    @BindView(R.id.iv_release)
    ImageView ivRelease;
    @BindView(R.id.stl_main)
    SlidingTabLayout stlMain;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.iv_shaixuan)
    View shaixuan;


    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_main_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        StatusBarUtil.setTranslucentForImageViewInFragment(mContext,0,getView());
        StatusBarUtil.setLightMode(mContext);
    }

    private String[] mTitle = new String[]{"动态", "关注","同城"};

    public void updateDongTai(DTBean obj){
        if(arrayList != null){
            if(arrayList.get(0) != null){
                ((DTFragment)arrayList.get(0)).updateDongTai(obj);
            }
            if(arrayList.get(1) != null){
                ((DTFragment)arrayList.get(1)).updateDongTai(obj);
            }
//            if(arrayList.get(2) != null){
//                ((CityMemberFragment)arrayList.get(2)).updateDongTai(obj);
//            }
        }
    }

    public void deleteDongTai(DTBean obj){
        if(arrayList != null){
            if(arrayList.get(0) != null){
                ((DTFragment)arrayList.get(0)).updateDongTai(obj);
            }
            if(arrayList.get(1) != null){
                ((DTFragment)arrayList.get(1)).updateDongTai(obj);
            }
            if(arrayList.get(2) != null){
                ((DTFragment)arrayList.get(2)).updateDongTai(obj);
            }

        }
    }

    public void updateDaCall(String memberId,int sfdc){
        if(arrayList != null){
            if(arrayList.get(0) != null){
                ((DTFragment)arrayList.get(0)).updateDaCall(memberId,sfdc);
            }
            if(arrayList.get(1) != null){
                ((DTFragment)arrayList.get(1)).updateDaCall(memberId,sfdc);
            }
            if(arrayList.get(2) != null){
                ((CityMemberFragment)arrayList.get(2)).updateDaCall(memberId,sfdc);
            }
        }
    }

    private RxDialogShaiXuan rxDialogShaiXuan;
    private String sex;
    private void showRxDialogShaiXuan() {
        if (rxDialogShaiXuan == null) {
            rxDialogShaiXuan = new RxDialogShaiXuan(mContext,R.style.dialogStyle);
            rxDialogShaiXuan.setAllClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sex = null;
                    rxDialogShaiXuan.getTv_all().setBackgroundResource(R.drawable.btn_orange_gradient_shape);
                    rxDialogShaiXuan.getTv_nv().setBackgroundResource(R.drawable.btn_gray_20_shape);
                    rxDialogShaiXuan.getTv_nan().setBackgroundResource(R.drawable.btn_gray_20_shape);
                }
            });
            rxDialogShaiXuan.setNvClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sex = "女";
                    rxDialogShaiXuan.getTv_all().setBackgroundResource(R.drawable.btn_gray_20_shape);
                    rxDialogShaiXuan.getTv_nv().setBackgroundResource(R.drawable.btn_orange_gradient_shape);
                    rxDialogShaiXuan.getTv_nan().setBackgroundResource(R.drawable.btn_gray_20_shape);

                }
            });
            rxDialogShaiXuan.setNanClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sex = "男";
                    rxDialogShaiXuan.getTv_all().setBackgroundResource(R.drawable.btn_gray_20_shape);
                    rxDialogShaiXuan.getTv_nv().setBackgroundResource(R.drawable.btn_gray_20_shape);
                    rxDialogShaiXuan.getTv_nan().setBackgroundResource(R.drawable.btn_orange_gradient_shape);
                }
            });
            rxDialogShaiXuan.setBtnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rxDialogShaiXuan.dismiss();
                    if (selectIndex == 0){
                        if(arrayList.get(0) != null){
                            ((DTFragment)arrayList.get(0)).shaixuan(sex);
                        }
                    }else{
                        if(arrayList.get(2) != null){
                            ((CityMemberFragment)arrayList.get(2)).shaixuan(sex);
                        }
                    }
                }
            });
        }
        rxDialogShaiXuan.show();
    }

    private int selectIndex = 0;
    private ArrayList<Fragment> arrayList = null;
    private void init() {
        arrayList = new ArrayList<>();
        arrayList.add(DTFragment.getInstance(0));
        arrayList.add(DTFragment.getInstance(1));
        arrayList.add(CityMemberFragment.getInstance(2));
        stlMain.setViewPager(vp, mTitle, mContext, arrayList);
        stlMain.getTitleView(0).setTextSize(20);
        stlMain.getTitleView(1).setTextSize(14);
        stlMain.getTitleView(2).setTextSize(14);
        stlMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if(position == 2){
                    shaixuan.setVisibility(View.VISIBLE);
                    if(arrayList.get(2) != null){
                        ((CityMemberFragment)arrayList.get(2)).shaixuan(null);
                    }
                }else if(position == 0){
                    shaixuan.setVisibility(View.VISIBLE);
                    if(arrayList.get(0) != null){
                        ((DTFragment)arrayList.get(0)).shaixuan(null);
                    }
                }else{
                    shaixuan.setVisibility(View.GONE);
                }
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTextSize(20);
                        //textView .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                        ////设置不为加粗
                        //textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTextSize(14);
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 2){
                    shaixuan.setVisibility(View.VISIBLE);
                    if(arrayList.get(2) != null){
                        ((CityMemberFragment)arrayList.get(2)).shaixuan(null);
                    }
                }else if(position == 0){
                    shaixuan.setVisibility(View.VISIBLE);
                    if(arrayList.get(0) != null){
                        ((DTFragment)arrayList.get(0)).shaixuan(null);
                    }
                }else{
                    shaixuan.setVisibility(View.GONE);
                }
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTextSize(20);
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTextSize(14);
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlMain.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.iv_shaixuan,R.id.iv_release})
    public void onViewClicked(View view) {
        if(view.getId() == R.id.iv_release){
            RxActivityTool.skipActivity(mContext, ReleaseDTActivity.class);
        }else{
            showRxDialogShaiXuan();
        }
    }
}
